<?php

class ScrapyAdmin extends AdminBase {

	private $nohf = ['stopscrapy'];

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->mysql = n_api('mysql')->cname('weixin');
	}

	public function index()
	{
		$article = n_api('article');
		$weixin = n_api('weixin');

		$data['weixinCount'] = $weixin->getWeixinCount();

		$data['articleDay'] = $article->getMaxArticleDay();
		$data['articleDaySum'] = $article->getArticleCountByDay($data['articleDay']);
		$data['articleAll'] = $article->getAllArticle();

		$this->_html('index', $data);
	}
	
	public function weixinList()
	{
		$param = $this->param('page:char:ignore');
		if (isset($param['page']))
			$page = $param['page'];
		else
			$page = 1;

		$perpage = 50;
		$weixin = n_api('weixin');

		$count = $weixin->getWeixinCount();
		$skip = ($page - 1) * $perpage;

		$data['weixinList'] = $weixin->getWeixinList($skip, $perpage,1,1);
		$data['paging'] = n_api('common')->paging(n_url('scrapy', 'weixinList'), $page, $count, $perpage);

		$this->_html('weixinList', $data);
	}

	public function stopScrapy()
	{
		$param = $this->param('id:char:empty');
		$id = $param['id'];

		$this->mysql->del('weixin_main', $id);
		n_mod("skin")->save("微信号已停止抓取",3);
	}

	public function articleList()
	{
		$param = $this->param('page:char:ignore');
		if (isset($param['page']))
			$page = $param['page'];
		else
			$page = 1;

		$perpage = 50;
		$article = n_api('article');

		$count = $article->getAllArticle();
		$skip = ($page - 1) * $perpage;

		$data['articleList'] = $article->getArticleList($skip, $perpage);
		$data['paging'] = n_api('common')->paging(n_url('scrapy', 'articleList'), $page, $count, $perpage);

		$this->_html('articleList', $data);
	}


	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}

}